package com.firewolf.rbac.controller;

import com.firewolf.rbac.PoiUtils;
import com.firewolf.rbac.params.UserParam;
import com.firewolf.rbac.pojo.User;
import com.firewolf.rbac.service.UserService;
import com.firewolf.rbac.vo.PageVO;
import com.firewolf.rbac.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;

/**
 * 作者：刘兴
 * 时间:19/2/28
 **/
@RestController
@Api(description = "用户操作接口")
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查找用户
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "查询用户列表")
    @GetMapping("/list")
    public PageVO<User> findUsers(UserParam param) {
        return userService.findList(param);
    }

    /**
     * 保存用户，新增或者是修改
     *
     * @param user
     * @return
     */
    @ApiOperation("保存用户")
    @PostMapping
    public ResponseVO saveUser(User user) {
        try {
            userService.saveUser(user);
        } catch (Exception e) {
            log.error("保存用户失败", e);
            return ResponseVO.error("保存用户失败");
        }
        return ResponseVO.ok("保存用户成功");
    }

    /**
     * 导出用户 信息到excel
     *
     * @return
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        try {
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("用户的.xls", "utf-8"));
            PoiUtils.exportToExcel(response.getOutputStream()
                    , Arrays.asList("id", "用户名", "邮箱", "手机号", "状态"), Arrays.asList("id", "name", "email", "mobile", "status"), userService.findAll());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/export2")
    public ResponseEntity<byte[]> export2() throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode("用户的.xls", "utf-8"));

        HttpStatus statusCode = HttpStatus.OK;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PoiUtils.exportToExcel(baos, Arrays.asList("id", "用户名", "邮箱", "手机号", "状态"), Arrays.asList("id", "name", "email", "mobile", "status"), userService.findAll());
        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(baos.toByteArray(), headers, statusCode);
        return response;

    }
}
